﻿using System.ServiceModel.Syndication;
using AutoMapper;
using HelperTools;
using Interfaces.FeedConverters;
using Interfaces.Models;
using NewsDataProvider.Mapping;

namespace NewsDataProvider.FeedConverter
{
    /// <summary>
    /// convert syndication feed to item that specification
    /// IFeed interface
    /// </summary>
    public class SyndicationFeedConverter : IFeedConverter<SyndicationFeed, IFeed>
    {
        /// <summary>
        /// convert method
        /// </summary>
        /// <param name="source">source syndication feed</param>
        /// <returns>target type</returns>
        public IFeed Convert(SyndicationFeed source)
        {
            SyndicationItemMappingCreator.Init();
            var result = Mapper.Map<SyndicationFeed, IFeed>(source);
            foreach (var item in result.With(x => x.Items))
                item.SourceFeed = result;
            return result;
        }
    }
}
